{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Plot multinomial and One-vs-Rest Logistic Regression\n\n\nPlot decision surface of multinomial and One-vs-Rest Logistic Regression.\nThe hyperplanes corresponding to the three One-vs-Rest (OVR) classifiers\nare represented by the dashed lines.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "print(__doc__)\n# Authors: Tom Dupre la Tour <tom.dupre-la-tour@m4x.org>\n# License: BSD 3 clause\n\nimport numpy as np\nimport matplotlib.pyplot as plt\nfrom sklearn.datasets import make_blobs\nfrom sklearn.linear_model import LogisticRegression\n\n# make 3-class dataset for classification\ncenters = [[-5, 0], [0, 1.5], [5, -1]]\nX, y = make_blobs(n_samples=1000, centers=centers, random_state=40)\ntransformation = [[0.4, 0.2], [-0.4, 1.2]]\nX = np.dot(X, transformation)\n\nfor multi_class in ('multinomial', 'ovr'):\n    clf = LogisticRegression(solver='sag', max_iter=100, random_state=42,\n                             multi_class=multi_class).fit(X, y)\n\n    # print the training scores\n    print(\"training score : %.3f (%s)\" % (clf.score(X, y), multi_class))\n\n    # create a mesh to plot in\n    h = .02  # step size in the mesh\n    x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1\n    y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1\n    xx, yy = np.meshgrid(np.arange(x_min, x_max, h),\n                         np.arange(y_min, y_max, h))\n\n    # Plot the decision boundary. For that, we will assign a color to each\n    # point in the mesh [x_min, x_max]x[y_min, y_max].\n    Z = clf.predict(np.c_[xx.ravel(), yy.ravel()])\n    # Put the result into a color plot\n    Z = Z.reshape(xx.shape)\n    plt.figure()\n    plt.contourf(xx, yy, Z, cmap=plt.cm.Paired)\n    plt.title(\"Decision surface of LogisticRegression (%s)\" % multi_class)\n    plt.axis('tight')\n\n    # Plot also the training points\n    colors = \"bry\"\n    for i, color in zip(clf.classes_, colors):\n        idx = np.where(y == i)\n        plt.scatter(X[idx, 0], X[idx, 1], c=color, cmap=plt.cm.Paired,\n                    edgecolor='black', s=20)\n\n    # Plot the three one-against-all classifiers\n    xmin, xmax = plt.xlim()\n    ymin, ymax = plt.ylim()\n    coef = clf.coef_\n    intercept = clf.intercept_\n\n    def plot_hyperplane(c, color):\n        def line(x0):\n            return (-(x0 * coef[c, 0]) - intercept[c]) / coef[c, 1]\n        plt.plot([xmin, xmax], [line(xmin), line(xmax)],\n                 ls=\"--\", color=color)\n\n    for i, color in zip(clf.classes_, colors):\n        plot_hyperplane(i, color)\n\nplt.show()"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.6.9"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}